//
//  OrderDetailViewHelper.m
//  ios-supplier
//
//  Created by feynmanyuan on 15/4/18.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//

#import "OrderDetailViewHelper.h"
@interface OrderDetailViewHelper(){
    NSMutableArray  *horizonLines;
}
@end

@implementation OrderDetailViewHelper

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = frame;
        horizonLines = [@[] mutableCopy];
    }
    return self;
}

-( void )addHorizontalLine:(NSUInteger)y color:(UIColor*)color isFull:(Boolean)isFull {
    [horizonLines addObject:[NSDictionary dictionaryWithObjects:@[[NSNumber numberWithInteger:y], color, isFull ? [NSNumber numberWithInt:1]:[NSNumber numberWithInt:0]]
                                                 forKeys:@[@"y", @"color", @"ifFull"]]];
}

- (void)drawRect:(CGRect)rect {
    // 1.获取上下文环境
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 1.0);
    
    for( NSDictionary *line in horizonLines ) {
        // 2.设置线的颜色和宽度
        CGContextSetStrokeColorWithColor(context, LineColorImportant.CGColor);
        
        CGContextBeginPath(context);
        CGContextSetLineCap(context, kCGLineCapRound);
        
        BOOL isFull = [(NSNumber*)[line objectForKey:@"isFull"] intValue] == 1;
        int horizonBeginX = isFull ? 0 : kOrderCellMargin;
        int horizonEndX = isFull ? self.bounds.size.width : self.bounds.size.width - kOrderCellMargin;
        
        int horizonY = [[line objectForKey:@"y"] intValue];
        CGContextMoveToPoint(context, horizonBeginX, horizonY);
        CGContextAddLineToPoint(context, horizonEndX, horizonY);
        
        CGContextClosePath(context);
        CGContextStrokePath(context);
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
